package com.itheima.service.impl;

import com.itheima.dao.CheckGroupDao;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.SetmealCheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public PageResult queryByPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        Integer begin = (currentPage - 1) * pageSize;
        List<CheckGroup> checkGroups = checkGroupDao.selectByCondition(begin, pageSize, queryString);
        Long total = checkGroupDao.totalCountByCondition(begin, pageSize, queryString);
        PageResult pageResult = new PageResult();
        pageResult.setRows(checkGroups);
        pageResult.setTotal(total);
        return pageResult;
    }

    @Override
    public void save(List<Integer> checkitemIds, CheckGroup checkGroup) {
        checkGroupDao.saveCheckGroup(checkGroup);
        String code = checkGroup.getCode();
        CheckGroup checkGroup1 = checkGroupDao.selectByCode(code);
        Integer id = checkGroup1.getId();//根据code获取对象继而获取到id

        if (checkitemIds != null && checkitemIds.size() != 0) {
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.saveItemId(id, checkitemId);//保存检查组id和每一个项目id
            }
        }

    }

    @Override
    public void deleteById(Integer id) {
        List<SetmealCheckGroup> scg = checkGroupDao.selectSetmealCheckgroupById(id);
        if (scg != null && scg.size() != 0) {
            throw new RuntimeException("该检查组正在被使用，不能删除");
        }
        checkGroupDao.deleteById(id);

    }

    @Override
    public Result getById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.getById(id);
        if (checkGroup == null) {
            throw new RuntimeException("查询异常!");
        }
        return Result.success(checkGroup, "查询检查组成功");
    }

    @Override
    public Result getItemIdsById(Integer id) {
        List<Integer> list = checkGroupDao.getItemIdsById(id);
        if (list == null) {
            throw new RuntimeException("查无检查项信息!");
        }
        return Result.success(list, "查询检查项成功");
    }

    @Override
    public void updateByIds(List<Integer> checkitemIds, CheckGroup checkGroup) {
        Integer id = checkGroup.getId();
        checkGroupDao.deleteItemsById(id);//删除旧的项目值
        checkGroupDao.updateCheckGroup(checkGroup);//更新检查组
        for (Integer checkitemId : checkitemIds) {
            checkGroupDao.saveItemId(id, checkitemId);//保存检查组id和每一个项目id
        }
    }

    @Override
    public Result selectAllGroup() {
        List<CheckGroup> checkGroups = checkGroupDao.selectAllGroup();
        return Result.success(checkGroups,"查询检查组成功");
    }
}
